<?php

namespace app\admin\controller\house;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 用户搬迁历史管理
 *
 * @icon fa fa-circle-o
 */
class History extends Backend
{

    /**
     * HouseHistory模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('HouseHistory');
        $this->view->assign('community',model('Community')->where(array('code'=>array('in',parent::getCommunityIdByAuth())))->field('code,name')->select());
    }

    public function index()
    {
        //设置过滤方法
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            return $this->handleSearch();
        }
        return $this->view->fetch();
    }


    private function handleSearch()
    {
        $append = array(
            array('community_code', 'in', parent::getCommunityIdByAuth())
        );
        list($where, $sort, $order, $offset, $limit, $orderParams) = $this->buildparams(null, null, $append);
        $total = $this->model->where($where)->count();
        $list = $this->model->with('community,house')->where($where)->order($orderParams)->limit($offset, $limit)->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }

    public function add()
    {
        return parent::create('form');
    }

    public function edit($ids = NULL)
    {
        return parent::modify($ids, 'form');
    }

}
